const path = require('path')

const viewsDir = path.join(path.dirname(__dirname), 'views')

// 导入模型
const query = require('../model/query.js')

const IndexController = {};

IndexController.index = (req, res) => {
    // res.sendFile(`${viewsDir}/index.html`)
    res.render('index.html')
}

IndexController.systemData = async (req, res) => {
    const sql = 'select * from settings';
    const rows = await query(sql)
    res.json(rows)
}



IndexController.login = (req, res) => {
    res.render(`login.html`)
}

IndexController.test = (req, res) => {
    res.render(`test.html`)
}

IndexController.systemData = async (req, res) => {
    const sql = 'select * from settings';
    const rows = await query(sql)
    res.json(rows)
}

// api数据接口
IndexController.apiData = (req, res) => {
    const myData = [{
            id: 1,
            title: '111',
            content: '11111',
            author: "显摆",
            status: 0,
            pic: "1.png",
            add_date: "2018",
            cate_id: 1
        },
        {
            id: 2,
            title: '222',
            content: '2222222',
            author: "显摆",
            status: 0,
            pic: "1.png",
            add_date: "2018",
            cate_id: 1
        },
        {
            id: 3,
            title: '333',
            content: '11111',
            author: "显摆",
            status: 0,
            pic: "1.png",
            add_date: "2018",
            cate_id: 1
        },
        {
            id: 4,
            title: '444',
            content: '11111',
            author: "显摆",
            status: 0,
            pic: "1.png",
            add_date: "2018",
            cate_id: 1
        },
        {
            id: 5,
            title: '555',
            content: '11111',
            author: "显摆",
            status: 0,
            pic: "1.png",
            add_date: "2018",
            cate_id: 1
        },
    ]
    res.json({
        code: 0,
        msg: "success",
        count: 100,
        data: myData
    })
}

IndexController.editPassword = async (req, res) => {
    res.render('editPassword.html')
}

//旧密码
IndexController.isoldPassword = async (req, res) => {
    let { password } = req.body;
    let { id } = req.params
    password = md5(`${password}${password_secret}`);
    const sql = `select password from users where id= ${id}`;
    const rows = await query(sql);
    const sqlPassword = rows[0].password;
    if (password === sqlPassword) {
        res.json({
            code: 0,
            message: '密码正确'
        })
    } else {
        res.json({
            code: 1,
            message: '密码错误'
        })
    }
}

//新密码
IndexController.updpassword = async (req, res) => {
    let { pass } = req.body;
    let { id } = req.params;
    pass = md5(`${pass}${password_secret}`);
    const sql = `update users set password = '${pass}' where id = ${id}`
    const rows = await query(sql);
    const { affectedRows } = rows
    if (affectedRows > 0) {
        req.session.destroy(function (err) {
            if (err) {
                throw err;
            }
        })
        res.json({
            code: 0,
            message: "修改成功"
        })
    } else {
        res.json({
            code: 1,
            message: "修改失败"
        })
    }
}


module.exports = IndexController;